package com.mvvm.ktnet.ui.recyclerView

import android.content.Context
import android.util.AttributeSet
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.constant.TimeConstants
import com.blankj.utilcode.util.TimeUtils
import java.text.SimpleDateFormat
import java.util.*


class SportHistorySelectMonth @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : RecyclerView(context, attrs, defStyleAttr) {


    private var itemWidth = 0

    val sportAdaper by lazy { TestAdapter() }

    val monthBeans = mutableListOf<SportMonthBean>()

    init {
        layoutManager = LinearLayoutManager(context).apply {
            orientation = HORIZONTAL
        }
        adapter = sportAdaper
        LinearSnapHelper().attachToRecyclerView(this)
        sportAdaper.setList(monthBeans)
        scrollToPosition(3)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        itemWidth = w / 5
        sportAdaper.setItemWidth(itemWidth)
    }

    fun setBindYear(bindData: Long) {
        //直接只需要前后加两个月
        setData(getDataSpan(bindData))
    }

    private fun setData(dataSpan: Int) {
        monthBeans.clear()
        for (i in dataSpan downTo 1) {
            val monthArray = getYearMonth(System.currentTimeMillis(), -i)
            monthBeans.add(SportMonthBean(monthArray[0], monthArray[1], false))
        }
        monthBeans.add(
            SportMonthBean(
                getYearMonth(System.currentTimeMillis(), 0)[0],
                getYearMonth(System.currentTimeMillis(), 0)[1],
                true
            )
        )
        for (i in 1..2) {
            val monthArray = getYearMonth(System.currentTimeMillis(), i)
            monthBeans.add(SportMonthBean(monthArray[0], monthArray[1], false))
        }
        sportAdaper.setList(monthBeans)
        scrollToPosition(dataSpan)
    }

    fun backToday() {
        val monthArray = getYearMonth(System.currentTimeMillis(), 0)
        if (!sportAdaper.data.isNullOrEmpty()) {
            for (item in sportAdaper.data) {
                item.select = monthArray[0] == item.year && monthArray[1] == item.month
            }
            sportAdaper.notifyDataSetChanged()
        }
    }
    //获取时间戳偏移的年月
    fun getYearMonth(time: Long, numMonth: Int): IntArray {
        val timeCal = Calendar.getInstance()
        timeCal.timeInMillis = time
        timeCal.add(Calendar.MONTH, numMonth)
        return intArrayOf(timeCal[Calendar.YEAR], timeCal[Calendar.MONTH] + 1)
    }
    //获取年月对应的偏差
    fun getDataSpan(bindData: Long): Int {
        val nowYear = getYearMonth(System.currentTimeMillis(), 0)[0]
        val nowMonth = getYearMonth(System.currentTimeMillis(), 0)[1]
        val bindYear = getYearMonth(bindData, 0)[0]
        val bindMonth = getYearMonth(bindData, 0)[1]
        //bindingData
        if (bindData - TimeUtils.getNowMills() >= 0) {
            return 2
        } else if (nowYear == bindYear && nowMonth == bindMonth) {
            return 2
        } else {
            val span = (nowYear - bindYear) * 12 - bindMonth + nowMonth
            return span
        }
    }

}





